package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserInfoService userInfoService;


    @PostMapping("loginReginfo")
   public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo
            , @RequestHeader("Authorization") String token) {
        //0、校验token的合法性
        //解析token
        //向userInfo中设置用户id
        userInfo.setId(UserHolder.getUserId());
        //调用service
        userInfoService.save(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     * 图像上传
     *
     * @param headPhoto
     * @param token
     * @return
     */
    @PostMapping("/loginReginfo/head")
    public ResponseEntity head(MultipartFile headPhoto, @RequestHeader("Authorization") String token) throws IOException {
        //0、校验token的合法性
        //解析token

        userInfoService.updae(headPhoto,UserHolder.getUserId());
        return ResponseEntity.ok(null);
    }
}
